package cn.hs.controller;

import cn.hs.entity.Heartmark;
import cn.hs.service.HeartmarkService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName HeartmarkController
 * @Description TODO
 * @Author mujiale
 * @Date 2024年10月23日 19:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/heartmark")
public class HeartmarkController {

    @Autowired
    private HeartmarkService heartmarkService;

    /**
     * 查询所有并分页
     * @param heartmark
     * @return
     */
    @GetMapping
    public Result select(
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "5")Integer pageSize,
            Heartmark heartmark){

        PageInfo<Heartmark> heartmarkPageInfo = heartmarkService.selectAll(pageNum, pageSize, heartmark);
        return Result.success(heartmarkPageInfo);
    }
    @PostMapping
    public Result insert(@RequestBody Heartmark heartmark){
        int num = heartmarkService.insert(heartmark);
        if (num != 0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
    @PutMapping
    public Result update(@RequestBody Heartmark heartmark){
        int num = heartmarkService.update(heartmark);
        if (num != 0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        int num = heartmarkService.delete(id);
        if (num != 0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/selectById")
    public Result selectById(@RequestParam Integer id){

        Heartmark heartmark = heartmarkService.selectById(id);
        return Result.success(heartmark);
    }
}
